/**
 * Candidate number: 35834
 */
package tv.starfu.sequencer.model;

import tv.starfu.utils.Music;

public class Pattern {

    private int length;
    private int numChannels;
    private int[][] grid;

    /**
     * Creates a new empty Pattern.
     * @param length    Number of steps.
     * @param numChannels   Number of channels.
     */
    public Pattern(int length, int numChannels) {
        this.length = length;
        this.numChannels = numChannels;
        createGrid();
    }

    /**
     * Creates an empty grid.
     */
    private void createGrid() {
        grid = new int[length][numChannels];
        for (int x = 0; x < length; ++x) {
            for (int y = 0; y < numChannels; ++y) {
                grid[x][y] = 1;
            }
        }
        set0to1();
    }
    
    /**
     * Sets channel 0 to default 0.
     */
    public void set0to1() {
      for(int i = 0; i < length; ++i) {
            grid[i][0] = 0;
        }
    }

    /**
     * Reset pattern values to zero.
     * @return int[][]  grid of zeroes.
     */
    public final int[][] reset() {
        for (int x = 0; x < length; ++x) {
            for (int y = 0; y < numChannels; ++y) {
                grid[x][y] = 0;
            }
        }
        return grid;
    }
    
    // TODO : test this
    /**
     * Get step values from the specified channel.
     * @param channelNum    Channel number.
     * @return  int[]       Array of step values.
     */
    public final int[] getChannel(int channelNum) {
        int[] channel = new int[length];
        for(int i = 0; i < length; ++i) {
            channel[i] = grid[i][channelNum];
        }
        return channel;
    }
    
     /**
     * Get channel values from the specified step.
     * @param stepNo    Step number.
     * @return  int[]   Array of channel values.
     */
    public final int[] getStep(int stepNo) {
        int[] step = new int[numChannels];
        for(int i = 0; i < numChannels; ++i) {
            step[i] = grid[stepNo][i];
        }
        return step;        
    }
    
    /**
     * Set the given cell of the grid to the specified phase.
     * @param phase    Phase
     * @param step     Cell step
     * @param channel  Cell channel
     */
    public final void setCellPhase(int step, int channel, int phase) {
        grid[step][channel] = phase;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getNumChannels() {
        return numChannels;
    }

    public void setNumChannels(int numChannels) {
        this.numChannels = numChannels;
    }
    
}
